package com.laikan.reader.core;

import android.app.ActivityManager;
import android.content.Context;
import android.view.Display;
import android.view.WindowManager;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.laikan.library.components.BasicApplication;
import com.laikan.library.utils.AndroidUtils;
import com.laikan.library.utils.DeviceUtil;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.comment.Constants;
import com.socks.library.KLog;

/**
 * 来看Application<br>
 *
 * @author wxl
 */

public class LaikanApplication extends BasicApplication {

    // 应用app版本号
    public static String mAppVersion;
    public static LaikanApplication applicationContext;

    /**
     * 是否是特殊包  true:是	false:否
     */
    public static int position = 0;
    public static String mess = "";
    public static Display disPlay;
    public static WindowManager windowManager;
    public static HomeOptions homeOptions;

    public synchronized static LaikanApplication getInstance() {
        return applicationContext;
    }
    public static Context context;

    public static HomeOptions getHomeOptions() {
        return homeOptions;
    }

    public static void setHomeOptions(HomeOptions homeOptions) {
        LaikanApplication.homeOptions = homeOptions;
    }

    @Override
    public void onCreate() {

        super.onCreate();
        this.context = this;
        if (Constants.PROCESS_NAME_LAIKANREADER.equals(getCurrentProcessName(this))) {

            mAppVersion = AndroidUtils.getCurrentAppVer(this);
            if (applicationContext == null) {
                applicationContext = this;
            }

            Fresco.initialize(this);
            SPUtil.getInstance().init(this, "laikan_sp");
            //SPUtil.getInstance().init(this, "tianhe_sp");
            if (!DeviceUtil.getMetaDataStringValue(this, "UMENG_CHANNEL").equals("test")) {
                KLog.init(false);   //debug 模式 关闭log
                //MobclickAgent.setDebugMode(true);// 友盟统计调试模式
            }else {
                KLog.init(true);
                //MobclickAgent.setDebugMode(false);
            }
            // LeakCanary.install(this);       // 配置捕获内存泄露
        }
    }

    public static Context getContext(){
        return context;
    }

    private String getCurrentProcessName(Context context) {
        int pid = android.os.Process.myPid();
        ActivityManager mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo appProcess : mActivityManager.getRunningAppProcesses()) {
            if (appProcess.pid == pid) {
                return appProcess.processName;
            }
        }
        return null;
    }
}
